<?php

// DB connection
//
//  Use without bind list:
//    $db = new DBConnection();
//    if ($db->executeQuery($query)) {
//        $result = $db->getResult();
//        if( ($result != null) && ($result->num_rows > 0) ) {
//            while($row = $result->fetch_assoc()) {
//                echo stripslashes($row['username']);	
//            }
//        }
//    }
//    
//  Use with bind list:
//    $db = new DBConnection();
//    if ($db->executeBindedQuery($query,"i",1)) {
//        $result = $db->getResult();
//        if( ($result != null) && ($result->num_rows > 0) ) {
//            while($row = $result->fetch_assoc()) {
//                echo stripslashes($row['username']);	
//            }
//        }
//    }
class DBConnection {

    private $db;
    private $connected = FALSE;
    
    private $db_user = 'root';
    private $db_name = 'game';
    private $db_password = '';
    private $db_host = '127.0.0.1';

    private $stmt;
    private $result;
    
    private $ErrorMSG;
    
    public function __construct()    {
        try {
            $this->connected = FALSE;
            
            $this->db = new mysqli( $this->db_host, $this->db_user, 
                                    $this->db_password, $this->db_name);
            /* check connection */
            if (mysqli_connect_error()) 
            {
                throw new Exception("Connect failed: %s\n", mysqli_connect_error());
            }
            $this->connected = TRUE;
        }
        catch(Exception $e)  {
            $this->ErrorMSG = $e->getMessage();
            die();
        }      
    }

    public function __destruct()  {
      if ($this->connected === TRUE) {
          $this->db->close();
      }
    }
   
    //get error msg
    public function getErrorMSG() {
        return $this->ErrorMSG;
    }    
    
    // executeQuery , require at least 3 args
    // Args = (Query , "sisid..." , [$var1,$var2...])
    // return true on succ , else false
    public function executeBindedQuery()  {
        try  {
            $args = func_get_args();
            //$count = count($args);

            $this->stmt = $this->db->stmt_init();
            
            // prepare statement query
            if( ! $this->stmt->prepare($args[0]) ) {
                //ERROR - Failed to prepare statement
                $this->ErrorMSG = "PrepStmtFaild";
                return false;
            }
            

            array_splice($args, 0, 1); // remove the  query from the args array (at index 0)

            foreach($args as &$v) // make new args array with Ref args
                $RefArg[] = &$v;
            
            // send the rest of the params (binding params)
            call_user_func_array(array($this->stmt , "bind_param"), $RefArg);  //$this->stmt->bind_param( $RefArg );
            $this->stmt->execute();
            $this->result = $this->stmt->get_result();

            return true;
        }
        catch(Exception $e) {
            $this->ErrorMSG = $e->getMessage();
            return false;
        }          
    }
    
    public function executeQuery($query)  {
        try {
            $this->result = $this->db->query($query);
            return true;
        }
        catch (Exception $e) {
            $this->ErrorMSG = $e->getMessage();
            return false;
        }
    }
    
    
    public function getResult() {
        return $this->result;
    }
    
    
    public function getLastInsertID() {
        return $this->db->insert_id;
    }

}